package gui;

import gui.tabs.ActionTab;
import gui.tabs.LogTab;
import gui.tabs.ProcessorTab;
import gui.tabs.TaskTab;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

import control.Generator;

import data.*;
import data.model.ProcAction;
import data.model.TaskGraph;
import data.mxgraph.mxTaskGraph;

/**
 * Contains methods for initialising other components and a acts as a container for the GUI and event handling components, contains a clipboard
 * 
 * @author Will Home
 *
 */

public class MainGUI extends JFrame {

	//CONSTANTS
    /** The default width of the main window */
    public static final int DEFAULT_WINDOW_WIDTH  = 1024;
    /** The default height of the main window */
    public static final int DEFAULT_WINDOW_HEIGHT = 334;
    /** The minimum width of the main window */
    public static final int MINIMUM_WINDOW_WIDTH  = 50;
    /** The minimum height of the main window. */
    public static final int MINIMUM_WINDOW_HEIGHT = 50;
    
    private Color backColour = new Color(155,205,155);
		
	private static MainGUI gui;
		
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		gui = new MainGUI(); 
	
		gui.initData();
		test();
		initOther();
		gui.initComponents();
		
		
		
		gui.pack();
		
		
	}
	
	private static void test() 
	{
		ActionList.addAction("act1");
		ActionList.addAction("act2");
		ActionList.addAction("act3");
		ActionList.getAction("act1").addTask(1);
		ActionList.getAction("act1").addTask(2);
		ActionList.getAction(1).addProc(1);
		ActionList.getAction(2).addProc(1);
		ActionList.getAction(2).addProc(2);
		
		TaskGraph.addTask("task1");
		TaskGraph.getTask(1).setAction(1);
		mxTaskGraph.addTask(TaskGraph.getTask(1));
		TaskGraph.addTask("task2");
		TaskGraph.getTask(2).setAction(1);
		mxTaskGraph.addTask(TaskGraph.getTask(2));
		TaskGraph.addDep(1, 2);
		mxTaskGraph.addDep(1, 2);
		mxTaskGraph.refresh();
		
		ProcList.addProc("proc1");
		ProcList.addProc("proc2");
		ProcList.getProc(1).addAction(new ProcAction(1));
		ProcList.getProc(1).addAction(new ProcAction(2));
		ProcList.getProc(2).addAction(new ProcAction(2));
		
		
		
	}

	//components
	private static ActionTab actions;
	private static ProcessorTab processors;
	private static TaskTab tasks;
	private static LogTab log;
		
	//data
	private static ActionList alist;
	private static ProcList plist;
	private static TaskGraph tgraph;
	private static mxTaskGraph mxtgraph;
	
	//other
	private static Generator gen;
	

	public MainGUI()
	{
		setTitle("Title");
		setLayout(new BorderLayout());
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	private void initData()
	{
		alist = new ActionList();
		plist = new ProcList();
		tgraph = new TaskGraph();
		mxtgraph = new mxTaskGraph();
	}
	
	private void initComponents()
	{
		setupMenu();
		setupView();
		setupEdit();
	}
	
	private static void initOther()
	{
		gen = new Generator();
	}
	
	private void setupMenu()
	{
		JMenuBar menuBar = new JMenuBar();
		
		GUIMenu fileMenu = new GUIMenu("File");
		
		menuBar.add(fileMenu);
		setJMenuBar(menuBar);
	}
	
	private void setupView()
	{
		JPanel viewPanel = new JPanel();
		JPanel subPanel = new JPanel();
		//viewPanel.setBackground(backColour);
		viewPanel.setLayout(new BorderLayout());
		viewPanel.setPreferredSize(new Dimension(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT));
		viewPanel.setMinimumSize(new Dimension(MINIMUM_WINDOW_WIDTH, MINIMUM_WINDOW_HEIGHT));
		
		//subPanel.setLayout(new BorderLayout());
		viewPanel.add(subPanel, BorderLayout.EAST);
		
		GraphView graphView = new GraphView(mxTaskGraph.getGraph());
		//graphView.setBackground(backColour);
		graphView.setPreferredSize(new Dimension(DEFAULT_WINDOW_WIDTH*3/4, DEFAULT_WINDOW_HEIGHT));
		graphView.setMinimumSize(new Dimension(MINIMUM_WINDOW_WIDTH/4, MINIMUM_WINDOW_HEIGHT/2));
		viewPanel.add(graphView, BorderLayout.WEST);
		
		JPanel actPanel = new ActionPanel();
		//actPanel.setBackground(backColour);
		actPanel.setPreferredSize(new Dimension(DEFAULT_WINDOW_WIDTH/8, DEFAULT_WINDOW_HEIGHT));
		actPanel.setMinimumSize(new Dimension(MINIMUM_WINDOW_WIDTH/10, MINIMUM_WINDOW_HEIGHT/2));
		//subPanel.add(actPanel, BorderLayout.EAST);
		subPanel.add(actPanel);
		
		JPanel procPanel = new ProcessorPanel();
		//procPanel.setBackground(backColour);
		procPanel.setPreferredSize(new Dimension(DEFAULT_WINDOW_WIDTH/8, DEFAULT_WINDOW_HEIGHT));
		procPanel.setMinimumSize(new Dimension(MINIMUM_WINDOW_WIDTH/10, MINIMUM_WINDOW_HEIGHT/2));
		//subPanel.add(procPanel, BorderLayout.EAST);
		subPanel.add(procPanel);
		
		getContentPane().add(viewPanel, BorderLayout.CENTER);
	}
			
	private void setupEdit()
	{
		JPanel editPanel = new JPanel();
		JTabbedPane tabs = new JTabbedPane();
		tabs.setTabPlacement(JTabbedPane.BOTTOM);
		
		actions = new ActionTab();
		processors = new ProcessorTab();
		tasks = new TaskTab();
		log = new LogTab();
		
		tabs.addTab("Actions", actions);
		tabs.addTab("Tasks", tasks);
		tabs.addTab("Processors", processors);
		tabs.addTab("LogTab", log);
		
		actions.setSrc(tabs);
		processors.setSrc(tabs);
		tasks.setSrc(tabs);
		//log.setSrc(tabs);
		
		tabs.addChangeListener(actions);
		tabs.addChangeListener(processors);
		tabs.addChangeListener(tasks);
				
		editPanel.setLayout(new BorderLayout());
		editPanel.setPreferredSize(new Dimension(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT));
		editPanel.setMinimumSize(new Dimension(MINIMUM_WINDOW_WIDTH, MINIMUM_WINDOW_HEIGHT));
		editPanel.add(tabs, BorderLayout.CENTER);
		
		getContentPane().add(editPanel, BorderLayout.SOUTH);
	}
}
